package view;

import entity.User;
import service.IUserService;
import service.UserServiceImpl;

import java.util.Scanner;

public class LoginView {
    private Scanner scanner = new Scanner(System.in);
    private IUserService userService = new UserServiceImpl();

    public void showLoginView() {
        while (true) { // 外层循环，确保退出登录后可以重新登录
            System.out.println("===== 登录界面 =====");
            System.out.print("请输入用户名：");
            String username = scanner.nextLine();
            System.out.print("请输入密码：");
            String password = scanner.nextLine();

            User user = userService.login(username, password);
            if (user != null) {
                System.out.println("登录成功！");
                // 根据用户类型进入相应视图
                if (user.getUserType().equals("admin")) {
                    new AdminView().showAdminView(user);
                } else if (user.getUserType().equals("manager")) {
                    new ManagerView().showManagerView(user);
                } else if (user.getUserType().equals("counterStaff")) {
                    new CounterStaffView().showCounterStaffView(user);
                } else {
                    System.out.println("未知用户类型，登录失败！");
                }
            } else {
                System.out.println("用户名或密码错误，请重新输入！");
            }

            System.out.print("是否继续登录？(y/n)：");
            String continueLogin = scanner.nextLine();
            if (continueLogin.equalsIgnoreCase("n")) {
                break;
            }
        }
    }
}